package giotto2D.filters.merge;

import giotto2D.filters.AbstractFilter;
import giotto2D.filters.color.Desaturate;
import java.awt.image.*;
import java.awt.*;

//--- Copies specified channel ( or desaturates image first ) of source to destination alpha.
public class LumaKey extends AbstractFilter
{

	//--- Do luma key
	public static void filter( 	BufferedImage alphaDest,
					BufferedImage keySource,
					int lowThreshold,
					int highThreshold,
					boolean invertKey )
	{
		if( invertKey )
			invertkey( alphaDest, keySource, lowThreshold, highThreshold );
		else
			normalkey( alphaDest, keySource, lowThreshold, highThreshold );
	}

	//--- normal lumakey, dark transparent, light opaque.
	private static void normalkey(  BufferedImage alphaDest,
					BufferedImage keySource,
					int lowThreshold,
					int highThreshold )
	{
		//--- Get rasters
		WritableRaster destRaster = alphaDest.getRaster();
		WritableRaster sourceRaster = keySource.getRaster();

		//--- Get intersecting dimensions.
		Dimension d = getIntersectionDimension( alphaDest, keySource );

		//--- Create pixel objects.
		int[] dPixel = new int[ 4 ];
		int[] sPixel = new int[ 4 ];
	
		int lumaValue;
		int alphaValue;
		int slopeWidth = highThreshold - lowThreshold;

		int i, j;

		//--- Draw loops.
		for( i = 0; i < d.width; i++ )
		{
			for( j = 0; j < d.height; j++ )
			{
				//--- Read pixels
				sourceRaster.getPixel( i, j, sPixel );
				destRaster.getPixel( i, j, dPixel );
				//--- get luma
				lumaValue = (( sPixel[ RED ] + sPixel[ BLUE ] + sPixel[ GREEN ] ) * 255 ) / MAX_RGB;
				//--- get alpha value
				if( lumaValue < lowThreshold + 1 )
					alphaValue = 0;
				else if( lumaValue > highThreshold -1 )
					alphaValue = 255;
				else
					alphaValue = ( ( lumaValue - lowThreshold ) / slopeWidth ) * 255;

				//--- Write alpha
				dPixel[ ALPHA ] = alphaValue;
				destRaster.setPixel( i, j, dPixel );
			}
		}

	}

	//--- key inverted, dark opaque, light transparent
	private static void invertkey(  BufferedImage alphaDest,
					BufferedImage keySource,
					int lowThreshold,
					int highThreshold )
	{
		//--- Get rasters
		WritableRaster destRaster = alphaDest.getRaster();
		WritableRaster sourceRaster = keySource.getRaster();

		//--- Get intersecting dimensions.
		Dimension d = getIntersectionDimension( alphaDest, keySource );

		//--- Create pixel objects.
		int[] dPixel = new int[ 4 ];
		int[] sPixel = new int[ 4 ];
	
		int lumaValue;
		int alphaValue;
		int slopeWidth = highThreshold - lowThreshold;

		int i, j;

		//--- Draw loops.
		for( i = 0; i < d.width; i++ )
		{
			for( j = 0; j < d.height; j++ )
			{
				//--- Read pixels
				sourceRaster.getPixel( i, j, sPixel );
				destRaster.getPixel( i, j, dPixel );
				//--- get luma
				lumaValue = ((sPixel[ RED ]+sPixel[ BLUE ]+sPixel[ GREEN ])*255)/MAX_RGB;
				//--- get alpha value
				if( lumaValue < lowThreshold + 1 )
					alphaValue = 255;
				else if( lumaValue > highThreshold -1 )
					alphaValue = 0;
				else
					alphaValue = 255 - ((lumaValue - lowThreshold) / slopeWidth ) * 255;

				//--- Write alpha
				dPixel[ ALPHA ] = alphaValue;
				destRaster.setPixel( i, j, dPixel );
			}
		}

	}
}//end class